package com.chushouya.manager.dao.repository;

import com.general.framework.core.lang.Strings;
import com.general.framework.data.mybatis.BaseRepository;
import com.chushouya.manager.dao.entity.UserPaymentMethodEntity;
import com.chushouya.manager.dao.mapper.UserPaymentMethodEntityMapper;
import com.chushouya.manager.dto.admin.payment.UserPaymentMethodQuery;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Objects;

@Repository
public class UserPaymentMethodRepository extends BaseRepository<UserPaymentMethodEntityMapper, UserPaymentMethodEntity, Long> {


    public UserPaymentMethodEntity selectOne(UserPaymentMethodQuery query) {
        return this.selectFirstOneByExample(this.buildExample(query));
    }

    public UserPaymentMethodEntity selectByUserId(Long userId) {
        UserPaymentMethodQuery query = new UserPaymentMethodQuery();
        query.setUserId(userId);
        return this.selectOne(query);
    }


    /**
     * 构建查询条件
     */
    private Example buildExample(UserPaymentMethodQuery query) {
        Example example = this.buildExample();
        
        if (Objects.nonNull(query.getUserId())) {
            example.and().andEqualTo("userId", query.getUserId());
        }
        
        if (Objects.nonNull(query.getPaymentType())) {
            example.and().andEqualTo("paymentType", query.getPaymentType());
        }
        
        if (Strings.isNotEmpty(query.getAccountName())) {
            example.and().andLike("accountName", Strings.format("%{}%", query.getAccountName()));
        }
        
        if (Strings.isNotEmpty(query.getAccountNumber())) {
            example.and().andLike("accountNumber", Strings.format("%{}%", query.getAccountNumber()));
        }
        
        if (Objects.nonNull(query.getStatus())) {
            example.and().andEqualTo("status", query.getStatus());
        }
        
        return example;
    }
}
